package com.mingda.weathermagiccenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.mingda.weathermagiccenter.pojo.WsEquisurfaceOrg;
import com.mingda.weathermagiccenter.pojo.WsStationData;
import com.mingda.weathermagiccenter.service.WsStationDataService;
import com.mingda.weathermagiccenter.mapper.WsEquisurfaceOrgMapper;
import com.mingda.weathermagiccenter.mapper.WsStationDataMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.text.html.parser.Entity;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【ws_station_data】的数据库操作Service实现
* @createDate 2024-12-10 08:44:56
*/
@Service
public class WsStationDataServiceImpl extends ServiceImpl<WsStationDataMapper, WsStationData>
    implements WsStationDataService{

    @Autowired
	WsStationDataMapper mapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateBatchById(List<WsStationData> list) {
        return SqlHelper.saveOrUpdateBatch(WsStationData.class, WsStationDataMapper.class, super.log, list, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {//这里主要是查询唯一约束对应的记录是否存在
            LambdaQueryWrapper<WsStationData> queryWrapper = Wrappers.<WsStationData>lambdaQuery()
                    .eq(WsStationData::getStationkey, entity.getStationkey()).eq(WsStationData::getTimebase,entity.getTimebase());
            Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
            map.put(Constants.WRAPPER, queryWrapper);
            return CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_LIST), map));
        }, (sqlSession, entity) -> {
            LambdaUpdateWrapper<WsStationData> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(WsStationData::getStationkey, entity.getStationkey()).eq(WsStationData::getTimebase,entity.getTimebase());
            Map<String, Object> param = CollectionUtils.newHashMapWithExpectedSize(2);
            param.put(Constants.ENTITY, entity);
            param.put(Constants.WRAPPER, lambdaUpdateWrapper);
            sqlSession.update(getSqlStatement(SqlMethod.UPDATE), param);
        });

    }

    @Override
	public List<Map<String, Object>> getWsStationData(String orgid, String stationtype, String timebase) {
		List<Map<String, Object>> result = mapper.getWsStationData(orgid, stationtype,timebase);
		return result;
	}
}




